local map = require('utils.vim').map
local b = require('utils.buf')
local M = {}

local function custom_find_files()
    local entry_display = require("telescope.pickers.entry_display")
    local displayer = entry_display.create {
        separator = "",
        items = {
            {}, -- colored path
            {}, -- plain filename
        },
    }

    local function make_display(entry)
        return displayer {
            { entry.path_part, "AlphabetPath" },
            { entry.filename },
        }
    end

    local entry_maker = function(entry)
        local path = entry:match("(.*/)")
        local filename = entry:match("([^/]+)$")
        path = path or ""
        filename = filename or entry
        return {
            value = entry,
            ordinal = entry,
            display = make_display,
            path_part = path,
            path = entry, -- telescope used to open the file
            filename = filename,
        }
    end

    return entry_maker
end

M.copy_path = function()
    local p = require("telescope.actions.state").get_selected_entry().path
    require('utils.editor').copy(p)
    require("notify")(p .. " copied.")
end
M.select_by_enter = function(prompt_bufnr)
    local actions = require("telescope.actions")
    local state = require("telescope.actions.state")
    local current = state.get_current_picker(prompt_bufnr)
    local v = current:_get_prompt()
    if string.len(v) > 0 then
        vim.fn.setreg("/",v) -- search term
    end
    actions.select_default(prompt_bufnr)
end
-- Thanks: https://github.com/nvim-telescope/telescope.nvim/issues/746
-- 按回车自动将文件内容拷贝到register(需要的地方自己paste)
local copy_file_content = function(path_to_file)
    local fn = string.gsub(path_to_file, " ", "\\ ")
    local content = vim.fn.systemlist('cat ' .. require('plenary/path'):new(fn):absolute())
    require('utils.editor').copy(content)
end
M.select_and_copy_file_content = function(prompt_bufnr, map, map_to_key)
    local function choices(close)
        local content = require('telescope.actions.state').get_selected_entry(prompt_bufnr)
        copy_file_content(content.cwd .. "/" .. content.value)
        require("notify")(content.value .. " snippet copied.")
        if close then
            require('telescope.actions').close(prompt_bufnr)
        end
    end
    if not map then
        return
    end
    map('i', map_to_key, function()
        choices(true)
    end)
    map('n', map_to_key, function()
        choices(true)
    end)
end

M.pick_recently_files = function()
    vim.g.flashed = 0
    require('telescope.builtin').oldfiles({
        attach_mappings = function(prompt_bufnr, map)
            M.select_and_copy_file_content(prompt_bufnr, map, '<M-i>')
            return true
        end
    })
end
M.pick_live_grep = function(is_visal)
    vim.g.flashed = 0
    if is_visal then
        local v = require('utils.editor').get_visual_selection()
        require('telescope.builtin').live_grep({
            default_text=v,
            --entry_maker = custom_live_grep_entry_maker(),
            attach_mappings = function(prompt_bufnr, map)
                M.select_and_copy_file_content(prompt_bufnr, map, '<M-i>')
                return true
            end
        })
        return
    end
    require('telescope.builtin').live_grep({
        --entry_maker = custom_live_grep,
        attach_mappings = function(prompt_bufnr, map)
            M.select_and_copy_file_content(prompt_bufnr, map, '<M-i>')
            return true
        end
    })
end
M.pick_find_files = function()
    vim.g.flashed = 0
    require('telescope.builtin').find_files({
        entry_maker = custom_find_files(),
        attach_mappings = function(prompt_bufnr, map)
            M.select_and_copy_file_content(prompt_bufnr, map, '<M-i>')
            return true
        end
    })
end
M.pick_file_browser = function(path_value)
    vim.g.flashed = 0
    if path_value then
        require('telescope').extensions.file_browser.file_browser({path=path_value,select_buffer=true})
    else
        require('telescope').extensions.file_browser.file_browser({path='%:p:h',select_buffer=true})
    end
end
M.pick_fuzzy_find = function(is_visal)
    vim.g.flashed = 0
    if is_visal then
        local v = require('utils.editor').get_visual_selection()
        require('telescope.builtin').current_buffer_fuzzy_find({default_text=v})
        return
    end
    require('telescope.builtin').current_buffer_fuzzy_find()
end
M.pick_snippets = function(use_find_files)
    vim.g.flashed = 0
    local p = '~/devhelper/mysnippets'
    if use_find_files then
        require('telescope.builtin').find_files({
            prompt_title = 'Snippets',
            preview_title = '',
            cwd = p,
            preview = true,
            attach_mappings = function(prompt_bufnr, map)
                M.select_and_copy_file_content(prompt_bufnr, map, '<CR>')
                return true
            end
        })
        return
    end
    require('telescope.builtin').live_grep({
        cwd = p,
    })
end
M.pick_info = function(use_find_files)
    vim.g.flashed = 0

    local p = '~/devhelper/myinfo'
    if use_find_files then
        require('telescope.builtin').find_files({
            prompt_title = 'Knowledge(js/py)',
            preview_title = '',
            cwd = p,
            preview = true,
            attach_mappings = function(prompt_bufnr, map)
                M.select_and_copy_file_content(prompt_bufnr, map, '<CR>')
                return true
            end
        })
        return
    end
    require('telescope.builtin').live_grep({
        cwd = p,
    })
end
M.pick_info_wx = function(use_find_files)
    vim.g.flashed = 0

    local p = '~/devhelper/myinfowx'
    if use_find_files then
        require('telescope.builtin').find_files({
            prompt_title = 'Knowledge(wx)',
            preview_title = '',
            cwd = p,
            preview = true,
            attach_mappings = function(prompt_bufnr, map)
                M.select_and_copy_file_content(prompt_bufnr, map, '<CR>')
                return true
            end
        })
        return
    end
    require('telescope.builtin').live_grep({
        cwd = p,
    })
end
M.pick_cmd_history = function()
    vim.g.flashed = 0
    require('telescope.builtin').command_history()
end
M.pick_key_maps = function()
    vim.g.flashed = 0
    require('telescope.builtin').keymaps()
end

return M
-- EOP
